package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Client;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.ClientService;
import com.woniuxy.factory.util.RegexUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ClientController {

    @Autowired
    ClientService clientService;
    //查询所有客户信息
    @ResponseBody
    @PostMapping("/client")
    public ResponseResult<List<Client>> getClients(){
        Client client = new Client();
        ResponseResult<List<Client>> responseResult=clientService.getClients(client);
        return responseResult;
    }
    //新增客户信息
    @ResponseBody
    @PostMapping("/addClient")
    public ResponseResult addClient(Client client){
        StringBuilder str = new StringBuilder();
        if (!RegexUtil.isMobile(client.getCliphone())){
            str=str.append("手机号不合法");
        }if (!RegexUtil.isEmail(client.getCliemail())){
            str=str.append("邮箱格式不合法");
            ResponseResult responseResult=new ResponseResult();
            responseResult.setMsg(str.toString());
            return responseResult;
        }
        ResponseResult responseResult=clientService.addClient(client);
        return responseResult;
    }
}
